package cn.wolfcode.p2p.base.service.impl;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.OrderTime;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.domain.VideoAuth;
import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.mapper.VideoAuthMapper;
import cn.wolfcode.p2p.base.query.QueryObject;
import cn.wolfcode.p2p.base.service.ILoginInfoService;
import cn.wolfcode.p2p.base.service.IOrderTimeService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.service.IVideoAuthService;
import cn.wolfcode.p2p.base.util.BitStateUtil;
import cn.wolfcode.p2p.base.util.Constants;
import cn.wolfcode.p2p.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class VideoAuthServiceImpl implements IVideoAuthService {
    @Autowired
    private VideoAuthMapper videoAuthMapper;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private ILoginInfoService loginInfoService;

    @Autowired
    private IOrderTimeService orderTimeService;

    @Override
    public int save(VideoAuth videoAuth) {
        return videoAuthMapper.insert(videoAuth);
    }

    @Override
    public int update(VideoAuth videoAuth) {
        return videoAuthMapper.updateByPrimaryKey(videoAuth);
    }

    @Override
    public VideoAuth get(Long id) {
        return videoAuthMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo queryList(QueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        List list = videoAuthMapper.selectList(qo);
        return new PageInfo(list);
    }

    /**
     *
     * @param timeId    时间的ID
     * @param orderDate 预约日期
     * @param auditorId   预约客服人员
     */
    @Override
    public void apply(Long timeId, String orderDate, Long auditorId) {
        //传过来的是非法参数 如果timeId 为null
        OrderTime orderTime = orderTimeService.get(timeId);
        if(orderTime == null){
            throw new DisplayableException("非法参数");
        }
        LoginInfo videoAudit = loginInfoService.get(auditorId);
        //如果传过来的auditId 找不到 抛出异常
        if(videoAudit == null){
            throw new DisplayableException("非法参数");
        }
        //如果传过来的后台管理员不是视频审核员 抛异常
        if (videoAudit.getIsAuditor() != LoginInfo.ISAUDITOR_YES) {
            throw new DisplayableException("非法参数");
        }
        //创建视频认证的对象
        VideoAuth videoAuth = new VideoAuth();
        //设置申请人 和 申请时间
        videoAuth.setApplier(UserContext.getCurrent());
        videoAuth.setApplyTime(new Date());

        //设置视频审核员
        videoAuth.setAuditor(videoAudit);

        //设置当前状态为审核状态
        videoAuth.setState(VideoAuth.STATE_NORMAL);

        //设置预约开始时间 和结束时间
        String orderBeginDateStr = orderDate + " " + orderTime.getBegin();
        String orderEndDateStr = orderDate + " " + orderTime.getEnd();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            videoAuth.setOrderBeginDate(sdf.parse(orderBeginDateStr));
            videoAuth.setOrderEndDate(sdf.parse(orderEndDateStr));
        } catch (ParseException e) {
            throw new DisplayableException("非法参数");
        }
        //保存到数据库中
        save(videoAuth);

        //更新userInfo 中videoAuthId 获取到videoAuth的ID并设值进去
        UserInfo current = userInfoService.getCurrent();
        current.setVideoAuthId(videoAuth.getId());
        userInfoService.update(current);
    }

    /**
     * 审核操作
     * @param videoAuthId  审核人ID
     * @param state 审核状态
     * @param remark   审核备注
     */
    @Override
    public void audit(Long videoAuthId, Integer state, String remark) {
        VideoAuth videoAuth = this.get(videoAuthId);
        //如果当前对象不为null 和其状态是待审核状态
        if(videoAuth != null && videoAuth.getState() == VideoAuth.STATE_NORMAL){
            //设置审核时间 和 审核内容
            videoAuth.setRemark(remark);
            videoAuth.setAuditTime(new Date());

            //获取申请人的ID
            UserInfo applyUserInfo = userInfoService.get(videoAuth.getApplier().getId());

            //如果state 和 审核通过的状态的值是一致的话
            if(state == VideoAuth.STATE_PASS){
                //并且改变视频审核的状态是通过状态
                videoAuth.setState(VideoAuth.STATE_PASS);
                //设置申请人的状态的是已通过视频审核
                applyUserInfo.addState(BitStateUtil.HAS_VIDEO_AUTH);
            }else{
                //并且改变视频审核的状态是拒绝状态
                videoAuth.setState(VideoAuth.STATE_REJECT);
                //设置申请人的状态为null
                applyUserInfo.setVideoAuthId(null);
            }
            //更新视频审核的更新内容
            this.update(videoAuth);
            //更新userInfo
            userInfoService.update(applyUserInfo);
        }
    }
}
